package com.scau.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.scau.service.ISerCateService;
import com.scau.vo.ServiceCategory;

/**
 * @author Jinglun
 * @create 2017年3月14日
 */
public class SerCateAction extends SuperAction {
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(AdminAction.class);
	private String result;// 属性result，用于异步传输数据交互
	
	private ISerCateService serCateService;
	private ServiceCategory serviceCategory;
	
	// 添加服务类型
	public String saveSerCate() {
		// 获得页面传过来的实体
		ServiceCategory sc = getServiceCategory();
		System.out.println("添加的类型名称："+sc.getName());

		if (serCateService.saveSerCate(sc)) {
			// 添加成功，检索所以类型返回
			List<ServiceCategory> scList = serCateService.listSerCate();
			JSONArray jsonArray = JSONArray.fromObject(scList.toString());
			result = jsonArray.toString();
			return "success";
		} else return "error";
		
	}

	// 查出所有服务类型，添加具体服务时调用到
	public String listSerCate2() {
		List<ServiceCategory> scList = serCateService.listSerCate();
		System.out.println("scList:"+scList);
		// 使用json方式返回值,toString必须
		JSONArray jsonArray = JSONArray.fromObject(scList.toString());
		result = jsonArray.toString();
		System.out.println(result);
		return "success";
	}
	
	// 这是另一种json返回方法。查出所有服务类型，添加具体服务时可调用，现在没有用
	public String listSerCate3() throws IOException {
		List<ServiceCategory> scList = serCateService.listSerCate();
		System.out.println("scList3:" + scList);
		// 使用json方式返回值,toString必须
		JSONArray jsonArray = JSONArray.fromObject(scList.toString());
		response.setCharacterEncoding("gbk");
		response.getWriter().print(jsonArray);
		return NONE;
	}
	
	// 删除服务类型
	public String deleteSerCate() {
		ServiceCategory sc = getServiceCategory();
		
		if (serCateService.deleteSerCate(sc.getId())) {
			List<ServiceCategory> serCateList = serCateService.listSerCate();
			JSONArray jsonArray = JSONArray.fromObject(serCateList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	// 更新服务类型
	public String updateSerCate(){
		ServiceCategory sc = getServiceCategory();
		System.out.println("json传来的id:"+sc.getId());
				
		if(serCateService.updateSerCate(sc)){
			System.out.println("测试能否进来这里："+sc.getRemark());
			List<ServiceCategory> serCateList = serCateService.listSerCate();
			JSONArray jsonArray = JSONArray.fromObject(serCateList.toString());
			result = jsonArray.toString();
			return "success";
		}else return "error";
				
	}

	/*// 模糊查询一条服务类型
	public String findSerCate() {
		list = serCateService.findSerCate(getMessage());
		// System.out.println("查询的列表为"+list);
		if(list != null) {
			return "success";
		}
		else return "error";
	}*/
		
	// get,set方法
	public ISerCateService getSerCateService() {
		return serCateService;
	}
	public void setSerCateService(ISerCateService serCateService) {
		this.serCateService = serCateService;
	}
	public ServiceCategory getServiceCategory() {
		return serviceCategory;
	}
	public void setServiceCategory(ServiceCategory serviceCategory) {
		this.serviceCategory = serviceCategory;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	
}
